<?php
namespace Home\Controller;
class TopicController extends HomeController {
    public function index()
    {
        import('ORG.Util.Page');
        $count = M('topic')->where('status=1')->count();
        $page = new \Think\Page($count,5);
        $show = $page->show();

        $data = M('topic')->table('dota2_topic t, dota2_user u,dota2_user_info ui')->field('t.t_id,t.title,t.type,t.reply_num,t.lasttime,ui.nickname,u.email')->limit($page->firstRow.','.$page->listRows)->order('lasttime desc')->where('t.status=1 and u.u_id=t.uid and u.u_id=ui.ui_id')->select();
        // $count = count($data);
        $this->assign('count',$count);
        $this->assign('data',$data);
        $this->assign('page',$show);
        $this->assign('first',$page->firstRow+1);
        $this->assign('last',min($page->firstRow+5,$count));
        // dump($data);
        $this->display();
    } 

    public function theme()
    {
        $data = M('topic')->table('dota2_topic t, dota2_user u,dota2_user_info ui')->field('u.u_id,t.content,t.title,ui.nickname,u.email,ui.icon,t.createtime')->where('u.u_id=t.uid and u.u_id=ui.ui_id and t.t_id='.$_GET['t'])->select()[0];
        $reply = D('reply');
        // var_dump($reply->getReply()['reply'][1]);
        // var_dump($data);
        $iconpath = '/Uploads/person_icon/';
        $iconpath .= substr($data['icon'],0,8).'/';
        $this->assign('iconpath',$iconpath);
        $this->assign('list',$reply->getReply()['reply']);
        $this->assign('page',$reply->getReply()['page']);
        $this->assign('data',$data);
        // var_dump($reply->getReply()['page']);
        $this->display();
    }

    public function add()
    {
        $title = $_POST['title'];
        $content = $_POST['content'];
        $data['title'] = $title; 
        $data['content'] = $content; 
        $data['uid'] = $_SESSION['uid']; 
        $data['createtime'] = time(); 
        $data['lasttime'] = time(); 
        if (!M('topic')->add($data)) {
            echo '1';
        }
    }

    public function reply()
    {
        $content = $_POST['content'];
        $data['content'] = $content; 
        $data['tid'] = $_GET['tid']; 
        $data['uid'] = $_SESSION['uid']; 
        $floor = M('reply')->field('max(floor)')->where('display=1 and tid='.$_GET['tid'])->select()[0]['max(floor)'];
        $data['floor'] = $floor + 1;
        // echo json_encode($floor);die;
        $data['time'] = time(); 
        if (empty(M('reply')->add($data))) {
            echo '1';die;
        }
        M('topic')->where('t_id='.$_GET['tid'])->setInc('reply_num');
        $update['lasttime'] = $data['time'];
        M('topic')->where('t_id='.$_GET['tid'])->save($update);
    }

    public function reply_floor()
    {   
        $content = $_POST['content'];
        $data['content'] = $content; 
        $data['tid'] = $_GET['tid'];
        $floor = $_POST['floor']; 
        // echo $floor;die;
        $rid = M('reply')->field('rid')->where('floor ='.$floor.' and tid='.$_GET['tid'])->select()[0]['rid'];
        // echo json_encode($rid);die;
        $data['rid'] = $rid;
        $data['uid'] = $_SESSION['uid'];
        $data['time'] = time(); 
        if (!M('reply_floor')->add($data)) {
            echo '1';
        }
        M('topic')->where('t_id='.$_GET['tid'])->setInc('reply_num');
        $update['lasttime'] = $data['time'];
        M('topic')->where('t_id='.$_GET['tid'])->save($update);
    }

    public function check_session()
    {
        if (session('uid')) {
            echo '1';
        }else{
            echo '0';
        }
    }

    public function topic_collect()
    {
        $data['uid'] = I('get.uid');
        $data['tid'] = I('get.tid');
        $res = M('topic_collect')->add($data);
        if ($res) {
            echo '1';
        }else{
            echo '0';
        }
    }

    public function topic_uncollect()
    {
        $map['uid'] = I('get.uid');
        $map['tid'] = I('get.tid');
        $res = M('topic_collect')->where($map)->delete();
        if ($res) {
            echo '1';
        }else{
            echo '0';
        }
    }

    public function check_collect()
    {
        $map['uid'] = I('get.uid');
        $map['tid'] = I('get.tid');
        $res = M('topic_collect')->where($map)->select();
        // echo M('topic_collect')->getLastSql();
        if ($res) {
            echo '1';
        }else{
            echo '0';
        }
    }

}